{{define "roles.add"}}
<!DOCTYPE html>
<html>
<style>
    #paigusu-screen {
        top: 20%;
    }
</style>
<head>
    <meta charset="utf-8">
    <title>角色添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/statics/third-pkg/layui/css/layui.css">
    <link rel="stylesheet" href="/statics/admin/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">key</label>
            <div class="layui-input-block">
                <input type="text" name="key"   placeholder="请输入标识"  lay-verify="required" lay-reqtext="key不能为空"  value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">菜单权限</label>
            <div class="layui-input-block">
                <div id="permission" class="xm-select"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="/statics/third-pkg/layui/layui.js"></script>
    <script src="/statics/admin/js/lay-config.js?v=2.0" charset="utf-8"></script>
    <script>

        layui.use(['form','request','layer','miniTab',"common","xmSelect"], function() {
            var form = layui.form,
                layer = layui.layer,
                miniTab = layui.miniTab,
                $=layui.$,
                xmSelect=layui.xmSelect,
                common=layui.common,
                request=layui.request;
            var demo1 = xmSelect.render({
                el: '#permission',
                name: 'permissions_id',
                layVerify: 'required',
                layVerType: 'msg',
                filterable: true,
                tree: {
                    //是否显示树状结构
                    show: true,
                    //是否展示三角图标
                    showFolderIcon: true,
                    indent: 20,
                    //默认展开节点的数组, 为 true 时, 展开所有节点
                    expandedKeys: true,
                    strict: true,
                    //是否开启极简模式
                    simple: true,
                },

                height: 'auto',
                theme: {
                    color: '#0081ff',
                },
                data: [
                ],
            })

            request.get('admin/permissions/list-tree').then(function (response) {
                if (response.data.code == 0) {
                    var lists=response.data.data.lists;
                    demo1.update({
                        data: lists,
                        autoRow: true,
                    })
                } else {
                    layer.msg(response.data.msg);
                }
            }).catch(function (error) {
                console.log(error)
                layer.msg('获取权限失败');
            })
            //监听提交
            form.on('submit(saveBtn)', function(data) {
                var value = demo1.getValue();
                var  permissions_id=value.map(function (item){
                    return item.value;
                }).join(',');
                var subData={
                    ...data.field,
                    permissions_id,
                }
                request.post('admin/roles/add',subData).then(function (response) {
                    if(response.data.code==0){
                        localStorage.setItem("roles",common.generateUUID())
                        layer.msg('添加角色成功',{
                            time:1000
                        },function (){
                            miniTab.deleteCurrentByIframe();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            window.location.href="/admin/roles/list";
                        })
                    }else{
                        layer.msg(response.data.msg);
                    }
                }).catch(function (error) {
                    console.log(error)
                    layer.msg('提交失败');
                });
            });

        });
    </script>
</body>

</html>
{{ end }}